﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciLib.utils {
    import flash.utils.*;

    public class ObjectPool {

        private static var objectList:Dictionary = new Dictionary();
        private static var objectStatusDic:Dictionary = new Dictionary();
        private static var objectIdleDic:Dictionary = new Dictionary();
        private static var objectClassDic:Dictionary = new Dictionary();

        public static function getAllDic():Object{
            var _local1:Object = {};
            _local1["objectList"] = objectList;
            _local1["objectStatusDic"] = objectStatusDic;
            _local1["objectIdleDic"] = objectIdleDic;
            _local1["objectClassDic"] = objectClassDic;
            return (_local1);
        }
        public static function getObjectInstance(_arg1:Class):Object{
            var _local5:Object;
            if (objectList[_arg1] == null){
                objectList[_arg1] = {
                    running:[],
                    idle:[]
                };
            };
            var _local2:Array = (objectList[_arg1]["running"] as Array);
            var _local3:Array = (objectList[_arg1]["idle"] as Array);
            if (_local3.length > 0){
                _local5 = _local3.shift();
                objectStatusDic[_local5] = true;
                _local2.push(_local5);
                return (_local5);
            };
            var _local4:Object = new (_arg1)();
            objectClassDic[_local4] = _arg1;
            objectStatusDic[_local4] = true;
            _local2.push(_local4);
            if (objectIdleDic[_arg1] == null){
                objectIdleDic[_arg1] = 5;
            };
            return (_local4);
        }
        public static function setMaxInstanceCount(_arg1:Class, _arg2:int):void{
            if (objectIdleDic[_arg1] == _arg2){
                return;
            };
            objectIdleDic[_arg1] = _arg2;
            clear(_arg1);
        }
        public static function idle(_arg1:Object):void{
            if (objectStatusDic[_arg1] != null){
                objectStatusDic[_arg1] = false;
            };
            clear((objectClassDic[_arg1] as Class));
        }
        public static function clear(_arg1:Class=null):void{
            var _local2:Object;
            if (_arg1 == null){
                for (_local2 in objectList) {
                    _clear((_local2 as Class));
                };
            } else {
                _clear(_arg1);
            };
        }
        private static function _clear(_arg1:Class):void{
            var _local2:int;
            var _local3:Array;
            var _local4:Array;
            var _local7:Object;
            var _local8:int;
            var _local9:Object;
            if (objectList[_arg1] == null){
                return;
            };
            _local2 = objectIdleDic[_arg1];
            _local3 = (objectList[_arg1]["running"] as Array);
            _local4 = (objectList[_arg1]["idle"] as Array);
            var _local5:int = _local3.length;
            var _local6:int;
            while (_local6 < _local5) {
                _local7 = _local3[_local6];
                if (_local7 != null){
                    if (objectStatusDic[_local7] == false){
                        _local4.push(_local7);
                        _local3[_local6] = null;
                    };
                };
                _local6++;
            };
            deleteArrayItemForValue(_local3, null);
            if (_local4.length > _local2){
                _local8 = _local2;
                while (_local8 < _local4.length) {
                    _local9 = _local4[_local8];
                    delete objectClassDic[_local9];
                    delete objectStatusDic[_local9];
                    _local8++;
                };
                _local4.length = _local2;
            };
        }
        public static function deleteObjectInstance(_arg1:Class):void{
            var _local2:Array;
            var _local3:int;
            var _local4:Object;
            if (objectList[_arg1] != null){
                _local2 = (objectList[_arg1] as Array);
                _local3 = 0;
                while (_local3 < _local2.length) {
                    _local4 = _local2[_local3];
                    delete objectStatusDic[_local4];
                    delete objectClassDic[_local4];
                    _local3++;
                };
                delete _local2[_arg1];
                delete objectIdleDic[_arg1];
            };
        }
        public static function deleteArrayItemForValue(_arg1:Array, _arg2:Object):Array{
            var _local5:Object;
            var _local3:int = _arg1.length;
            var _local4:int;
            var _local6:int;
            while (_local6 < _local3) {
                if (_arg1[_local6] != _arg2){
                    _local5 = _arg1[_local4];
                    _arg1[_local4] = _arg1[_local6];
                    _arg1[_local6] = _local5;
                    _local4 = (_local4 + 1);
                };
                _local6++;
            };
            _arg1.length = _local4;
            return (_arg1);
        }
        public static function hasObject(_arg1:Object):Boolean{
            if (((!((objectStatusDic[_arg1] == null))) && ((objectStatusDic[_arg1] == true)))){
                return (true);
            };
            return (false);
        }

    }
}//package com.fminutes.vinciLib.utils 
